package com.huidao.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.huidao.entity.Goods;
import com.huidao.entity.Order;
import com.huidao.entity.Role;
import com.huidao.entity.User;
import com.huidao.util.JDBCUtil;

public class OrderDaoImpl implements OrderDao {

	Connection conn = null;
	PreparedStatement ps =null;
	ResultSet rs =null;
	
	@Override
	public boolean buyOrder(Order order) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO `order`(GOODS_ID,GOODS_NUM,USER_ID) VALUES(?,?,?)";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, order.getGoods().getGoodsId());
			ps.setLong(2, order.getGoodsNum());
			ps.setLong(3, order.getUser().getUserId());
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public boolean payOrder(Order order) {
		// TODO Auto-generated method stub
		
		String sql = "UPDATE `order` SET ORDER_STATE=1 WHERE ORDER_ID=?";
		
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, order.getOrderId());
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public boolean sendOrder(Order order) {
		// TODO Auto-generated method stub

		String sql = "UPDATE `order` SET ORDER_STATE=2 WHERE ORDER_ID=?";
		
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, order.getOrderId());
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}
	
	@Override
	public boolean overOrder(Order order) {
		// TODO Auto-generated method stub

		String sql = "UPDATE `order` SET ORDER_STATE=3 WHERE ORDER_ID=?";
		
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, order.getOrderId());
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public List<Order> getListPage(int currentPage, int pageSize, Long id) {
		// TODO Auto-generated method stub
		
		List<Order> list = new ArrayList<Order>();
		String sql = "SELECT * FROM `order` O LEFT JOIN  GOODS G ON O.GOODS_ID=G.GOODS_ID "
				+ " LEFT JOIN USER s ON s.user_ID=G.user_ID LEFT JOIN USER b ON b.user_ID=o.user_ID where 1=1 ";
		if(id!=null&&id!=0){
			sql = sql+" AND O.USER_ID="+id;
		}
		sql+=" limit ?,?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (currentPage-1)*pageSize);
			ps.setInt(2, pageSize);
			rs =ps.executeQuery();
			while(rs.next()){
				Order order = new Order();
				order.setOrderId(rs.getLong("ORDER_ID"));
				order.setGoodsNum(rs.getLong("GOODS_NUM"));
				order.setOrderState(rs.getLong("ORDER_STATE"));
				Goods goods = new Goods();
				goods.setGoodsId(rs.getLong("GOODS_ID"));
				goods.setGoodsName(rs.getString("GOODS_NAME"));
				goods.setGoodsPrice(rs.getDouble("GOODS_PRICE"));
				
				User user = new User();
				user.setUserName(rs.getString("USER_NAME"));
				user.setUserShopname(rs.getString("user_shopname"));
				user.setUserPhone(rs.getLong("user_phone"));
				goods.setUser(user);
				order.setGoods(goods);
				
				User user1 = new User();
				user1.setUserId(rs.getLong("USER_ID"));
				order.setUser(user1);
				list.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

	@Override
	public int getTotalCount(Long id) {
		// TODO Auto-generated method stub
		String sql = "SELECT COUNT(*) FROM ORDER WHERE 1=1";
		if(id!=null&&id!=0){
			sql = sql+" AND USER_ID="+id;
		}
		
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs =ps.executeQuery();
			if(rs.next()){
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return 0;
	}

	@Override
	public List<Order> getAllList() {
		// TODO Auto-generated method stub
		List<Order> list = new ArrayList<Order>();
		String sql = "SELECT * FROM `order` O LEFT JOIN  GOODS G ON O.GOODS_ID=G.GOODS_ID "
				+ " LEFT JOIN USER s ON s.user_ID=G.user_ID LEFT JOIN USER b ON b.user_ID=o.user_ID";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs =ps.executeQuery();
			while(rs.next()){
				Order order = new Order();
				order.setOrderId(rs.getLong("ORDER_ID"));
				order.setGoodsNum(rs.getLong("GOODS_NUM"));
				order.setOrderState(rs.getLong("ORDER_STATE"));
				Goods goods = new Goods();
				goods.setGoodsId(rs.getLong("GOODS_ID"));
				goods.setGoodsName(rs.getString("GOODS_NAME"));
				goods.setGoodsPrice(rs.getDouble("GOODS_PRICE"));
				User user = new User();
				user.setUserName(rs.getString("USER_NAME"));
				user.setUserShopname(rs.getString("user_shopname"));
				user.setUserPhone(rs.getLong("user_phone"));
				goods.setUser(user);
				order.setGoods(goods);
				list.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

	
}
